package com.yong.ssj.db.dao.customer;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.yong.ssj.db.dto.customer.CustomerDto;
import com.yong.ssj.db.dto.customer.MaterialDto;
import com.yong.ssj.db.dto.sys.UserDto;
import com.yong.ssj.vo.customer.MaterialVo;
import com.yong.ssj.vo.customer.RenzhenVo;
import com.yong.ssj.vo.customer.UpdategerenVo;
import com.yong.ssj.vo.customer.UpdatejiaVo;
import com.yong.ssj.vo.customer.jiashiVo;
import com.yong.ssj.vo.customer.twiceDrVo;
import com.yong.ssj.vo.customer.twiceIdVo;

@Repository(value="OrdinaryDaoImpl")
public class OrdinaryDaoImpl implements IOrdinaryDao{
	@Resource
	private JdbcTemplate jdbcTemplate;

	RowMapper<MaterialDto> mRowMapper = new RowMapper<MaterialDto>(){

		@Override
		public MaterialDto mapRow(ResultSet arg0, int arg1) throws SQLException {
			MaterialDto materialDto = new MaterialDto();
			materialDto.setBank_card_1(arg0.getString("bank_card_1"));
			materialDto.setBank_card_1_status(arg0.getString("bank_card_1_status"));
			materialDto.setBank_card_2(arg0.getString("bank_card_2"));
			materialDto.setBank_card_2_status(arg0.getString("bank_card_2_status"));
			materialDto.setContact_file(arg0.getString("contact_file"));
			materialDto.setContact_file_status(arg0.getString("contact_file_status"));
			materialDto.setCustomer_id(arg0.getInt("customer_id"));
			materialDto.setDriving_license_1(arg0.getString("driving_license_1"));
			materialDto.setDriving_license_1_status(arg0.getString("driving_license_1_status"));
			materialDto.setDriving_license_2(arg0.getString("driving_license_2"));
			materialDto.setDriving_license_2_status(arg0.getString("driving_license_2_status"));
			materialDto.setIdcard_1(arg0.getString("idcard_1"));
			materialDto.setIdcard_1_status(arg0.getString("idcard_1_status"));
			materialDto.setIdcard_2(arg0.getString("idcard_2"));
			materialDto.setIdcard_2_status(arg0.getString("idcard_2_status"));
			materialDto.setMaterial_id(arg0.getInt("material_id"));
			materialDto.setPhone_service_code(arg0.getString("phone_service_code"));
			materialDto.setRz_money_limit(arg0.getDouble("rz_money_limit"));
			materialDto.setRz_status(arg0.getString("rz_status"));
			materialDto.setRz_surplus_money(arg0.getDouble("rz_surplus_money"));
			materialDto.setRz_type(arg0.getString("rz_type"));
			return materialDto;
		}
		
		
	};
	
	
	@Override
	public List<Map<String, Object>> queryOrdinary() {
		// TODO Auto-generated method stub
		String sql = "select * from tb_customer where customer_status='0'";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public MaterialDto queryCustomer(long customer_id) {
		// TODO Auto-generated method stub
		String sql ="select * from tb_material where customer_id=?" ;
		MaterialDto materialDto = null;
		try {
			materialDto = jdbcTemplate.queryForObject(sql, new Object[]{customer_id}, mRowMapper);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return materialDto;
	}

	@Override
	public List<Map<String, Object>> queryWait() {
		// TODO Auto-generated method stub
		String sql ="select a.*,b.* from tb_customer a,tb_material b where b.rz_status!='2' and a.customer_id=b.customer_id";
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		list = jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public List<Map<String, Object>> queryAuthenticated() {
		// TODO Auto-generated method stub
		String sql ="select a.*,b.* from tb_customer a,tb_material b where b.rz_status='2' and a.customer_id=b.customer_id";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public int updateMaterial(MaterialVo vo) {
		// TODO Auto-generated method stub
		StringBuffer sql = new StringBuffer("update tb_material set ");
		sql.append("idcard_1_status=?,idcard_2_status=?,driving_license_1_status=?,driving_license_2_status=?,");
		sql.append("bank_card_1_status=?,bank_card_2_status=?,contact_file_status=? ");
		sql.append("where customer_id=?");
		List<Object> args = new ArrayList<Object>();
		args.add(vo.getIdcard_1_status());
		args.add(vo.getIdcard_2_status());
		args.add(vo.getDriving_license_1_status());
		args.add(vo.getDriving_license_2_status());
		args.add(vo.getBank_card_1_status());
		args.add(vo.getBank_card_2_status());
		args.add(vo.getContact_file_status());
		int yan = jdbcTemplate.update(sql.toString(),args.toArray());
		return yan;
	}

	@Override
	public CustomerDto queryPhone(String customer_name) {
		// TODO Auto-generated method stub
		String sql = "select * from tb_customer where customer_name=?";
		CustomerDto customerDto = null;
		try {
			customerDto = jdbcTemplate.queryForObject(sql, new Object[]{customer_name}, new BeanPropertyRowMapper<CustomerDto>(CustomerDto.class));
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			return customerDto;
			}
	}

	@Override
	public String queryPhone(long customer_id) {//查询手机号
		// TODO Auto-generated method stub
		String sql = "select * from tb_customer where customer_id=?";
		CustomerDto dto = jdbcTemplate.queryForObject(sql, new Object[]{customer_id}, new BeanPropertyRowMapper<CustomerDto>(CustomerDto.class));
		return dto.getCustomer_name();
	}

	@Override
	public int updateGR(UpdategerenVo vo) {
		// TODO Auto-generated method stub
		StringBuffer sql = new StringBuffer("update tb_material set ");
		sql.append("idcard_1_status=?,idcard_2_status=?,");
		sql.append("bank_card_1_status=?,bank_card_2_status=?,contact_file=?,contact_file_status=?,rz_money_limit=?,rz_surplus_money=? ");
		sql.append("where material_id=?");
		List<Object> args = new ArrayList<Object>();
		args.add(vo.getIdcard_1_status());
		args.add(vo.getIdcard_2_status());
		args.add(vo.getBank_card_1_status());
		args.add(vo.getBank_card_2_status());
		args.add(vo.getContact_file());
		if(vo.getContact_file()==null){
			args.add("0");
		}else {
			args.add("1");
		}
		args.add(Double.valueOf(vo.getRz_money_limit()));
		args.add(Double.valueOf(vo.getRz_money_limit()));
		args.add(Integer.parseInt(vo.getMaterial_id()));
		int yan = jdbcTemplate.update(sql.toString(),args.toArray());
		return yan;
	}

	@Override
	public int updateJS(UpdatejiaVo vo) {
		StringBuffer sql = new StringBuffer("update tb_material set ");
		sql.append("driving_license_1_status=?,driving_license_2_status=?,");
		sql.append("bank_card_1_status=?,bank_card_2_status=?,contact_file=?,contact_file_status=?,rz_money_limit=?,rz_surplus_money=? ");
		sql.append("where material_id=?");
		List<Object> args = new ArrayList<Object>();
		args.add(vo.getDriving_license_1_status());
		args.add(vo.getDriving_license_2_status());
		args.add(vo.getBank_card_1_status());
		args.add(vo.getBank_card_2_status());
		args.add(vo.getContact_file());
		if(vo.getContact_file()==null){
			args.add("0");
		}else {
			args.add("1");
		}
		args.add(Double.valueOf(vo.getRz_money_limit()));
		args.add(Double.valueOf(vo.getRz_money_limit()));
		args.add(Integer.parseInt(vo.getMaterial_id()));
		int yan = jdbcTemplate.update(sql.toString(),args.toArray());
		return yan;
	}

	@Override
	public int updateStatus(String rz_status,String material_id) {
		// TODO Auto-generated method stub
		String sql = "update tb_material set rz_status=? where material_id=?";
		int yan = jdbcTemplate.update(sql,new Object[]{rz_status,Integer.parseInt(material_id)});
		return yan;
	}

	@Override
	public List<Map<String, Object>> materialStatus(long customer_id) {//查询认证状态
		// TODO Auto-generated method stub
		String sql = "select * from tb_material where customer_id=?";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,new Object[]{customer_id});
		return list;
	}

	@Override
	public int updateGRuse(RenzhenVo vo, long customer_id) {
		// TODO Auto-generated method stub
		StringBuffer sql = new StringBuffer("update tb_customer set ");
		sql.append("true_name=?,email=?,address=?,id_card=?,zfb_code=?,wx_code=?,yl_code=?,customer_sex=?");
		sql.append("where customer_id=?");
		List<Object> args = new ArrayList<Object>();
		args.add(vo.getTrue_name());
		args.add(vo.getEmail());
		args.add(vo.getAddress());
		args.add(vo.getId_card());
		args.add(vo.getZfb_code());
		args.add(vo.getWx_code());
		args.add(vo.getYl_code());
		args.add(vo.getCustomer_sex());
		args.add(customer_id);
		int yan = jdbcTemplate.update(sql.toString(),args.toArray());
		return yan;
	}


	@Override
	public int updateGRma(RenzhenVo vo, long customer_id) {
		// TODO Auto-generated method stubStringBuffer sql = new StringBuffer("update tb_customer set ");
		StringBuffer sql = new StringBuffer("update tb_material set ");
		sql.append("phone_service_code=?,idcard_1=?,idcard_2=?,bank_card_1=?,bank_card_2=?,rz_status=?,rz_type=?");
		sql.append("where customer_id=?");
		List<Object> args = new ArrayList<Object>();
		args.add(vo.getPhone_service_code());
		args.add(vo.getIdcard_1());
		args.add(vo.getIdcard_2());
		args.add(vo.getBank_card_1());
		args.add(vo.getBank_card_2());
		args.add("1");
		args.add("1");
		args.add(customer_id);
		int yan = jdbcTemplate.update(sql.toString(),args.toArray());
		return yan;
	}
	
	@Override
	public int updateJSuse(jiashiVo vo, long customer_id) {
		// TODO Auto-generated method stub
		StringBuffer sql = new StringBuffer("update tb_customer set ");
		sql.append("true_name=?,email=?,address=?,id_card=?,zfb_code=?,wx_code=?,yl_code=?,customer_sex=?");
		sql.append("where customer_id=?");
		List<Object> args = new ArrayList<Object>();
		args.add(vo.getTrue_name());
		args.add(vo.getEmail());
		args.add(vo.getAddress());
		args.add(vo.getId_card());
		args.add(vo.getZfb_code());
		args.add(vo.getWx_code());
		args.add(vo.getYl_code());
		args.add(vo.getCustomer_sex());
		args.add(customer_id);
		int yan = jdbcTemplate.update(sql.toString(),args.toArray());
		return yan;
	}

	@Override
	public int updateJSma(jiashiVo vo, long customer_id) {
		StringBuffer sql = new StringBuffer("update tb_material set ");
		sql.append("phone_service_code=?,driving_license_1=?,driving_license_2=?,bank_card_1=?,bank_card_2=?,rz_status=?,rz_type=?");
		sql.append("where customer_id=?");
		List<Object> args = new ArrayList<Object>();
		args.add(vo.getPhone_service_code());
		args.add(vo.getDriving_license_1());
		args.add(vo.getDriving_license_2());
		args.add(vo.getBank_card_1());
		args.add(vo.getBank_card_2());
		args.add("1");
		args.add("2");
		args.add(customer_id);
		int yan = jdbcTemplate.update(sql.toString(),args.toArray());
		return yan;
	}

	@Override
	public int updateziliao(String zfb_code, String wx_code, String address, long customer_id) {
		// TODO Auto-generated method stub
		String sql = "update tb_customer set zfb_code=?,wx_code=?,address=? where customer_id=?";
		int yan = jdbcTemplate.update(sql,new Object[]{zfb_code,wx_code,address,customer_id});
		return yan;
	}

	@Override
	public CustomerDto queryCuser(long customer_id) {
		// TODO Auto-generated method stub
		String sql ="select * from tb_customer where customer_id=?" ;
		CustomerDto dto = jdbcTemplate.queryForObject(sql, new Object[]{customer_id}, new BeanPropertyRowMapper<CustomerDto>(CustomerDto.class));
		return dto;
	}

	@Override
	public int updatePassword(String customer_password, long customer_id) {
		// TODO Auto-generated method stub
		String sql = "update tb_customer set customer_password=? where customer_id=?";
		int yan = jdbcTemplate.update(sql,new Object[]{customer_password,customer_id});
		return yan;
	}

	@Override
	public int updateTwiceID(twiceIdVo vo, long customer_id) {
		// TODO Auto-generated method stub
		String sql = "update tb_material set idcard_1=?,idcard_2=?,bank_card_1=?,bank_card_2=?,rz_status=? where customer_id=?";
		int yan = jdbcTemplate.update(sql, new Object[]{vo.getIdcard_1(),vo.getIdcard_2(),vo.getBank_card_1(),vo.getBank_card_2(),"1",customer_id});
		return yan;
	}

	@Override
	public int updateTwiceDr(twiceDrVo vo, long customer_id) {
		// TODO Auto-generated method stub
		String sql = "update tb_material set driving_license_1=?,driving_license_2=?,bank_card_1=?,bank_card_2=?,rz_status=? where customer_id=?";
		int yan = jdbcTemplate.update(sql, new Object[]{vo.getDriving_license_1(),vo.getDriving_license_2(),vo.getBank_card_1(),vo.getBank_card_2(),"1",customer_id});
		return yan;
	}

	@Override
	public int updateCusStatus(String customer_status, long customer_id) {
		// TODO Auto-generated method stub
		String sql = "update tb_customer set customer_status=? where customer_id=?";
		int yan = jdbcTemplate.update(sql,new Object[]{customer_status,customer_id});
		return yan;
	}
	
	
}
